﻿using System;
using System.Collections;
using System.Configuration;
using System.Data;
using System.Linq;
using System.Web;
using System.Web.Security;
using System.Web.UI;
using System.Web.UI.HtmlControls;
using System.Web.UI.WebControls;
using System.Web.UI.WebControls.WebParts;
using System.Xml.Linq;
using System.Text;
using System.Xml.Serialization;
using System.IO;
using System.Collections.Generic;

public partial class SendDataToClient : System.Web.UI.Page
{
    protected void Page_Load(object sender, EventArgs e)
    {
        //send data to the client
        string domain = URL.getDomain(Request.Url.ToString());
        string domainName = URL.getDomainName(Request.Url.ToString());
        string domainSuf = URL.getDomainSuf(Request.Url.ToString());
        string directory;
        string filename;
        string flag = "";

        string paraStr = Request.QueryString["paraStr"];

        // if (domainSuf.IndexOf("www") > -1)//domainSuf may be "//www".
        // {
        //     domainSuf = domainName;
        // }
        // else 
        if (Request.Url.ToString().IndexOf("localhost") > -1)
        {
            domainSuf = "localhost";
        }

        //在本地调试时，domainSuf 为空
        if (!Directory.Exists(MapPath("") + "//userdata//" + domainSuf) || domainSuf == "")//如果没有此用户
        {
            if(!string.IsNullOrEmpty(paraStr))
            {
                domainSuf = paraStr.Substring(1, paraStr.Length - 1); ;
            }

            if(!Directory.Exists(MapPath("") + "//userdata//" + domainSuf) || domainSuf == "")
            {
                domainSuf = "www";//把默认数据传给用户.
            }
        }
        if (Request.QueryString != null && Request.QueryString["flag"] != null)
        {
            if (Request.QueryString["flag"] == "main")
            {
                flag = Request.QueryString["flag"];
                directory = "//" + flag;
                filename = MapPath("") + "//userdata//" + domainSuf + directory + "//sendData.json";
                LOG.writer("send the content from " + filename + "to the client");
                Response.WriteFile(filename);
                return;
            }
            else if (Request.QueryString["flag"] == "private")
            {
                if (Request.Cookies["username"]!=null&&!string.IsNullOrEmpty(Request.Cookies["username"].ToString()))
                {
                    string currentDir = MapPath("") + "//userdata//" + Request.Cookies["username"].Value + "//";
                    user userInstance = new user(currentDir + "user.xml");
                    //if the username and check code is correct, then send data to the client.
                    if (Request.Cookies["username"] != null && Request.Cookies["checkCode"].Value == userInstance.Cc)
                    {
                        flag = Request.QueryString["flag"];
                        directory = "//" + flag;
                        filename = MapPath("") + "//userdata//" + Request.Cookies["username"].Value + directory + "//sendData.json";
                        LOG.writer("send the content from " + filename + "to the client");
                        Response.WriteFile(filename);
                        return;
                    }
                }
                else
                {
                    if(string.IsNullOrEmpty(paraStr))
                    {
                        paraStr = "www";
                    }
                    string currentDir = MapPath("") + "//userdata//" + paraStr + "//";
                    user userInstance = new user(currentDir + "user.xml");
                    flag = Request.QueryString["flag"];
                    directory = "//" + flag;
                    filename = MapPath("") + "//userdata//" + paraStr + directory + "//sendData.json";
                    LOG.writer("send the content from " + filename + "to the client");
                    Response.WriteFile(filename);
                    return;

                }
            }
            directory = "//" + flag;
        }

        if (Request.QueryString != null && Request.QueryString["flag"] == "public")
        {
            LOG.writer(MapPath("") + "//userdata//public//main//sendData.json");
            Response.WriteFile(MapPath("") + "//userdata//public//main//sendData.json");
            return;
        }
    }

    private void CopyFolder(string from, string to)
    {
        if (!Directory.Exists(to))
            Directory.CreateDirectory(to);        // 子文件夹        
        foreach (string sub in Directory.GetDirectories(from))
            CopyFolder(sub + "\\", to + Path.GetFileName(sub) + "\\");
        // 文件
        foreach (string file in Directory.GetFiles(from))
            File.Copy(file, to + Path.GetFileName(file), true);
    }
}
